﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.Repistory;
using U.B.WriteApi.Command;

namespace U.B.WriteApi.CommandHander
{
    public class DelLevaCommandHander : IRequestHandler<DelLevaCommand, ApiResult<string>>
    {
        private readonly IRepistory<LeaveApp> laRep;
        private readonly IRepistory<User> userRep;
        public DelLevaCommandHander(IRepistory<LeaveApp> laRep, IRepistory<User> userRep)
        {
            this.laRep = laRep;
            this.userRep = userRep;
        }
        public Task<ApiResult<string>> Handle(DelLevaCommand request, CancellationToken cancellationToken)
        {
            ApiResult<string> result = new ApiResult<string>();
            //审批后不可删除
            var a = laRep.GetAll().FirstOrDefault(x => x.LAId == request.LAId);
            if (a != null)
            {
                if (a.StatusId == (int)Status.待审批)
                {
                    a.IsDeleted = true;
                    laRep.Update(a);
                    result. code = Renum.删除成功;
                    result.Message = "删除成功";
                    result.Data = result.Message;

                }
                else
                {
                    result.code = Renum.删除失败;
                    result.Message = "已审批，不可删除";
                    result.Data = result.Message;

                }

            }
            else
            {
                result.code = Renum.删除失败;
                result.Message = "删除失败";
                result.Data = result.Message;
            }
            return Task.FromResult(result);
        }
    }
}
